import 'dart:async';
import 'dart:convert'; //模型转换需要用到这个类
import 'package:http/http.dart' as http;
import 'package:imitatectripapp/model/travel_model.dart';

const TRAVEL_TAB_URL =
    'http://www.devio.org/io/flutter_app/json/travel_page.json';

// 旅拍页接口
// 接口的固定参数
var Params = {
  "districtId": -1,
  "groupChannelCode": "RX-OMF",
  "type": null,
  "lat": -180,
  "lon": -180,
  "locatedDistrictId": 0,
  "pagePara": {
    "pageIndex": 1,
    "pageSize": 10,
    "sortType": 9,
    "sortDirection": 0
  },
  "imageCutType": 1,
  "head": {'cid': "09031014111431397988"},
  "contentType": "json"
};

class TravelDao {
  // ignore: non_constant_identifier_names
  static Future<TravelItemModel> GETTravelData(
      String url, String groupChannelCode, int pageIndex, int pageSize) async {
    Map paramsMap = Params['pagePara'];
    paramsMap['pageIndex'] = pageIndex;
    paramsMap['pageSize'] = pageSize;
    Params['groupChannelCode'] = groupChannelCode;
    final response = await http.post(url, body: jsonEncode(Params));
    if (response.statusCode == 200) {
      // 解决中文乱码
      Utf8Decoder utf8decoder = Utf8Decoder();
      var result = json.decode(utf8decoder.convert(response.bodyBytes));
      return TravelItemModel.fromJson(result);
    } else {
      print('请求接口失败');
      throw Exception('请求接口失败');
    }
  }
}
